// src/api/chat/user.ts
import  {request } from '@/utils/request'

export interface SysUser {
  id?: number
  account: string
  passwordHash?: string
  nickname: string
  avatarUrl?: string
  status?: number // 1-正常，0-停用
  lastSeenAt?: string
  createdAt?: string
  updatedAt?: string
}

export interface SysUserVo {
  id: number
  account: string
  nickname: string
  avatarUrl?: string
  status?: number
  lastSeenAt?: string
  createdAt?: string
  updatedAt?: string
  // 可按后端实际增加 token、角色等
}

export interface RegisterParams {
  account: string
  passwordHash: string
  nickname: string
}

export interface LoginParams {
  account: string
  passwordHash: string
}

export interface ProfileParams {
  userId: number
}

export interface SysUserPageParams {
  account?: string
  status?: number // 1/0
  pageNum: number // 从 1 开始
  pageSize: number
}

export interface BoPage<T> {
  pageNum: number
  pageSize: number
  total: number
  pages: number
  list: T[]
}

/**
 * 用户注册
 * POST /api/chat/user/register
 * @param {RegisterParams} data account、passwordHash、nickname
 * @returns {Promise<any>} JsonResult<SysUser>
 */
export function registerUser(data: RegisterParams): Promise<any> {
  return request({
    url: '/api/chat/user/register',
    method: 'post',
    data,
  })
}

/**
 * 用户登录
 * POST /api/chat/user/login
 * @param {LoginParams} data account、passwordHash
 * @returns {Promise<any>} JsonResult<SysUserVo>
 */
export function loginUser(data: LoginParams): Promise<any> {
  return request({
    url: '/api/chat/user/login',
    method: 'post',
    data,
  })
}

/**
 * 用户详情
 * GET /api/chat/user/profile
 * @param {ProfileParams} params { userId }
 * @returns {Promise<any>} JsonResult<SysUser>
 */
export function getUserProfile(params: ProfileParams): Promise<any> {
  return request({
    url: '/api/chat/user/profile',
    method: 'get',
    params,
  })
}

/**
 * 用户保活
 * GET /api/chat/user/profile
 *
 * @param {ProfileParams} params { userId }
 */
export function getKeepAlive(params: ProfileParams) {
  return request({
    url: '/api/chat/user/keepAlive',
    method: 'get',
    params,
  })
}

/**
 * 用户在线状态
 * GET /api/chat/user/profile
 * @param {ProfileParams} params { userId }
 * @returns {Promise<any>} JsonResult<SysUser>
 */
export function getOnlineInfo() {
  return request({
    url: '/api/chat/user/onlineInfo',
    method: 'get',
  })
}

/**
 * 新增用户（管理）
 * POST /api/chat/user
 * @param {SysUser} data SysUser JSON
 * @returns {Promise<any>} JsonResult<number> 新建主键ID
 */
export function createSysUser(data: SysUser): Promise<any> {
  return request({
    url: '/api/chat/user',
    method: 'post',
    data,
  })
}

/**
 * 根据ID查询（管理）
 * GET /api/chat/user/{id}
 * @param {number|string} id 用户ID
 * @returns {Promise<any>} JsonResult<SysUser>
 */
export function getSysUserById(id: number | string): Promise<any> {
  return request({
    url: `/api/chat/user/${id}`,
    method: 'get',
  })
}

/**
 * 更新用户（管理）
 * PUT /api/chat/user/{id}
 * @param {number|string} id 用户ID
 * @param {SysUser} data SysUser JSON
 * @returns {Promise<any>} JsonResult<boolean>
 */
export function updateSysUser(id: number | string, data: SysUser): Promise<any> {
  return request({
    url: `/api/chat/user/${id}`,
    method: 'put',
    data,
  })
}

/**
 * 删除用户（管理）
 * DELETE /api/chat/user/{id}
 * @param {number|string} id 用户ID
 * @returns {Promise<any>} JsonResult<boolean>
 */
export function deleteSysUser(id: number | string): Promise<any> {
  return request({
    url: `/api/chat/user/${id}`,
    method: 'delete',
  })
}

/**
 * 分页查询（管理）
 * GET /api/chat/user/page
 * @param {SysUserPageParams} params { account?, status?, pageNum, pageSize }
 * @returns {Promise<any>} JsonResult<BoPage<SysUser>>
 */
export function pageSysUsers(params: SysUserPageParams): Promise<any> {
  return request({
    url: '/api/chat/user/page',
    method: 'get',
    params,
  })
}
